package org.asiainfo.system.dubbo;

import cn.hutool.core.date.DateUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.asiainfo.common.core.exception.user.UserException;
import org.asiainfo.common.core.utils.MapstructUtils;
import org.asiainfo.system.api.RemoteUserService;
import org.asiainfo.system.api.domain.vo.RemoteUserVo;
import org.asiainfo.system.api.model.LoginUser;
import org.asiainfo.system.domain.SysUser;
import org.asiainfo.system.domain.vo.SysUserVo;
import org.asiainfo.system.service.SysUserService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户服务
 *
 * @author dotor-ww
 */
@Service
@DubboService
@RequiredArgsConstructor
@Slf4j
public class RemoteUserServiceImpl implements RemoteUserService {

    private final SysUserService sysUserService;

    /**
     * 通过用户id查询用户信息
     *
     * @param userId   用户ID
     * @param tenantId 租户ID
     * @return 结果
     */
    @Override
    public LoginUser getUserInfo(Long userId, Long tenantId) throws UserException {
        return sysUserService.getUserInfo(userId, tenantId);
    }

    /**
     * 通过用户名查询用户信息
     *
     * @param username 用户名
     * @return 结果
     */
    @Override
    public LoginUser getUserInfo(String username) throws UserException {
        return sysUserService.getUserInfo(username);
    }

    /**
     * 通过用户ID查询用户信息
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public RemoteUserVo getUserInfoById(Long userId) throws UserException {
        SysUserVo sysUserVo = sysUserService.selectUserId(userId);
        return MapstructUtils.convert(sysUserVo, RemoteUserVo.class);
    }

    /**
     * 更新用户信息
     *
     * @param userId   用户ID
     * @param tenantId 租户ID
     * @param ip       IP地址
     */
    @Override
    public void recordLoginInfo(Long userId, Long tenantId, String ip) {
        SysUser sysUser = new SysUser();
        sysUser.setUserId(userId);
        sysUser.setLoginIp(ip);
        sysUser.setLoginIp(ip);
        sysUser.setLoginDateTime(DateUtil.date());
        sysUser.setUseLastTenantId(tenantId);
        sysUser.setUpdateBy(userId);
        sysUserService.updateById(sysUser);
    }

    /**
     * 通过用户名查询用户昵称
     *
     * @param userId 用户ID
     * @return 用户昵称
     */
    @Override
    public String selectNickNameByUserId(Long userId) {
        return sysUserService.getNickNameByUserId(userId);
    }

    /**
     * 通过用户ID查询用户名
     *
     * @param userId 用户ID
     * @return 用户名
     */
    @Override
    public String selectNameByUserId(Long userId) {
        return sysUserService.getUserNameByUserId(userId);
    }

    /**
     * 通过用户ID列表查询用户昵称列表
     *
     * @param userIds 用户ID列表
     * @return 用户昵称列表
     */
    @Override
    public List<String> selectNickNameByUserIdList(List<String> userIds) {
        List<Long> userIdList = userIds.stream()
            .map(Long::parseLong)
            .collect(Collectors.toList());
        return sysUserService.selectNickNameByUserIdList(userIdList);
    }
}
